Step 13: Supertest
To test the API endpoints, in addition to Vitest, we will use SuperTest:
yarn add -D supertest
SuperTest is an HTTP assertions library that allows you to test your HTTP servers. We primarily use it to run the API server without actually running the server!
Let's write a simple test that uses SuperTest library! Add an index.test.js
to the tests
folder with the following content:
import { test, expect } from "vitest";
import app from "../src/index.js";
import supertest from "supertest";
const request = new supertest(app);
test("Test API / endpoint", async () => {
const response = await request.get("/");
expect(response.status).toBe(200);
});
We use supertest
to wrap the express app
. It allows us to send a request to the API without actually running the server.
Next, tun the tests with yarn test
. Notice they all pass, including the one we just added.
Save and commit all changes.